<template>
	<web-view ref="webview" :src="src" @message="h5PostMessage" @onPostMessage="h5PostMessage"></web-view>
</template>

<script>
	const initSrc = "https://m.51bmj.cn/"
	export default {
		data() {
			return {
				src: initSrc,
				token: true,
				h5Data: {},
				isShare: false
			}
		},
		methods: {
			h5PostMessage(e) {
				for(var i=0;i<e.detail.data.length;i++){
					if(i == e.detail.data.length-1){
						this.h5Data = e.detail.data[i]
					}
				}
			}
		},
		onLoad(option) {
			if (option.redirect) { //通过分享进入的跳转
				this.src = decodeURIComponent(option.redirect)
			}else{
				this.src = initSrc
			}
		},
		onHide() {
			this.token = false
		},
		onShow() {
			if(this.isShare){
				this.isShare = false
				return
			}
			if (this.token) {
				return
			}
			let token = uni.getStorageSync('token');
			let openid = uni.getStorageSync('openId');
			if (token && openid) {
				this.token = true
				if ((this.src.indexOf('#') == -1)) {
					console.log("change")
					this.src = initSrc + "pages/login/index#token=" + token + "&openid=" + openid+"&random="+Math.random();
					//this.src = this.src+"pages/login/index?openid="+openid
				}else{
					this.src = this.src.replace('random','random1')
				}
				
			}
			//uni.setStorageSync('token', res1.message);
			//uni.setStorageSync('openId', res.result.openid);
			// this.$nextTick(()=>{
			//   this.src = this.src+"pages/login/index#aaaaaa"
			// })

		},
		//转发
		onShareAppMessage(res) {
			this.isShare = true
			console.log('触发分享',this.h5Data,'参数')
			let imgUrl = 'https://m.51bmj.cn/static/bmj.jpg'
			if(this.h5Data.imgUrl){
				imgUrl = this.h5Data.imgUrl
			}
			let title = '比目镜'
			if(this.h5Data.shareTitle){
				title = this.h5Data.shareTitle
			}
			if(!this.h5Data.imgUrl || this.h5Data.imgUrl == null){
				this.h5Data.imgUrl = undefined
			}else{
				this.h5Data.imgUrl = this.h5Data.imgUrl.replace('http://','https://')
			}
			let shareInfo = {
				path: this.h5Data.link, //这是为了传参 onload(data){let id=data.id;}
				// title: this.h5Data.shareTitle,
				title: title,
				imageUrl: this.h5Data.imgUrl
			}
			this.h5Data = {}
			return shareInfo;
		}
	}
</script>

<style>

</style>
